package com.lingyuan.warehouse.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingyuan.common.core.domain.R;
import com.lingyuan.common.core.domain.vo.wms.WmsPurchaseDetailVO;
import com.lingyuan.common.core.domain.warehouse.pojo.PO.WmsPurchaseDetail;
import com.lingyuan.warehouse.config.RedisConssion;
import com.lingyuan.warehouse.domain.WmsPurchase;
import com.lingyuan.warehouse.domain.WmsWarePO;
import com.lingyuan.warehouse.mapper.WmsPurchaseDetailMapper;
import com.lingyuan.warehouse.service.WmsPurchaseDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;


/**
 * @Author：寇可往，我亦可往。
 * @Package：com.lingyuan.warehouse.service.impl
 * @Project：lingyuan-shopping-mall
 * @name：WmsPurchaseDetailServiceimpl
 * @Date：2024/9/19 21:18
 */
@Service
public  class WmsPurchaseDetailServiceimpl extends ServiceImpl<WmsPurchaseDetailMapper, WmsPurchaseDetail>
                                                                              implements WmsPurchaseDetailService{


    @Autowired
    private WmsPurchaseDetailMapper wmsPurchaseDetailMapper;
    @Autowired
    private RedisConssion redisConssion;




    @Override
    @Transactional
    public void add(List<WmsPurchaseDetail> wmsPurchaseDetail) {
        if (wmsPurchaseDetail.size () <=0){
        }else {
            //TODO 添加
            //添加之后预警表也跟着改变
            wmsPurchaseDetailMapper.insertAdd ( wmsPurchaseDetail );
            wmsPurchaseDetailMapper.WmsPurchaseUpdate (wmsPurchaseDetail );
            redisConssion.initWareSku ();
        }
    }

    @Override
    public void update(WmsPurchaseDetail wmsPurchaseDetail) {
        wmsPurchaseDetailMapper.updateById ( wmsPurchaseDetail );
        redisConssion.initWareSku ();
    }

    @Override
    public void delete(Long id) {
        wmsPurchaseDetailMapper.deleteById ( id );
        redisConssion.initWareSku ();

    }

    @Override
    public List<WmsPurchase> getPuchaseList(WmsPurchaseDetailVO wmsPurchaseDetailVO) {
        return wmsPurchaseDetailMapper.getPuchaseList(wmsPurchaseDetailVO);
    }

    @Override
    public void purchaseAdd(WmsPurchase purchase) {

        if (purchase== null){
            throw new RuntimeException ( "不可为空" );
        }
        Long skuNum = purchase.getSkuNum ();
        String skuName = purchase.getSkuName ();
        WmsWarePO wmsWarePO = wmsPurchaseDetailMapper.wmsWareName(skuName);
        if (wmsWarePO==null){
            throw new RuntimeException ( "没有找到该存货" );
        }
        wmsPurchaseDetailMapper .purchaseAdd ( purchase );
        wmsPurchaseDetailMapper.updateSkuId ( skuNum,wmsWarePO.getId () );
        redisConssion.initWareSku ();

    }

    @Override
    @Transactional
    public void purchaseAllUpdate(WmsPurchase wmsPurchaseDetail) {
       wmsPurchaseDetailMapper.updateId ( wmsPurchaseDetail );
       wmsPurchaseDetailMapper.updateIdUp ( wmsPurchaseDetail.getId());
       wmsPurchaseDetailMapper.wasreAdd(wmsPurchaseDetail);
       redisConssion.initWareSku ();

    }

    @Override
    public void mergePurchase(List<WmsPurchaseDetailVO> wmsPurchaseDetailVO) {
       wmsPurchaseDetailMapper.mergePurchase ( wmsPurchaseDetailVO );
    }

}
